package com.baikeyang.app.controller.admin.activity;

import com.baikeyang.common.bean.PageResponseResult;
import com.baikeyang.common.bean.PageResult;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.bean.activity.type.ActivityType;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.Dictionaries;
import com.baikeyang.common.util.StringUtils;
import com.baikeyang.service.activity.IActivityTypeService;
import com.baikeyang.service.dictionaries.IDictionariesService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 活动类型管理Controller
 */
@RestController(value = "activityTypeController")
@RequestMapping(value = "/admin/activity/type")
public class ActivityTypeController {

	@Autowired
	private IActivityTypeService activityTypeService;

	@RequestMapping(value = "index")
	public ModelAndView index () {
		return new ModelAndView("/admin/activity/type/activity-type-index");
	}


	@RequestMapping(value = "list")
	public Object getActivityTypes (String page, String recPerPage, String search, String sortBy, String order) {
		PageResult pagers = new PageResult();
		try{
			Integer size = 10;
			Integer current = 1;
			if(StringUtils.isNotEmpty(recPerPage)){
				try {
					size = Integer.parseInt(recPerPage);
				} catch (Exception e) {}
			}
			if(StringUtils.isNotEmpty(page)) {
				try {
					current = Integer.parseInt(page);
					if(current < 1){current = 1;}
				} catch (Exception e) {}
			}
			Page<Dictionaries> pageParam = new Page<>(current, size);
			IPage<Dictionaries> pageResult = activityTypeService.getTypeAllPage(pageParam);
			pagers.setData(pageResult.getRecords());
			pagers.setPager(current, pageResult.getTotal());
			pagers.setResult("success");
		}catch (Exception e) {
			e.printStackTrace();
		}
		return pagers;
	}

	@RequestMapping(value = "add")
	public ModelAndView add () {
		return new ModelAndView("/admin/activity/type/activity-type-add");
	}

	@RequestMapping(value = "edit/{id}")
	public ModelAndView edit (@PathVariable int id) {
		return new ModelAndView("/admin/activity/type/activity-type-edit")
			.addObject("activityType", activityTypeService.getActivityType(id));
	}

	@RequestMapping(value = "save")
	public ResponseResult save (ActivityType type) {
		return activityTypeService.saveActivityType(type);
	}

	@RequestMapping(value = "update")
	public ResponseResult update (ActivityType type) {
		return activityTypeService.updateActivityType(type);
	}

	@RequestMapping(value = "delete/{id}")
	public ResponseResult delete (@PathVariable int id) {
		return activityTypeService.deleteActivityType(id);
	}

	@RequestMapping(value = "all")
	public Object getActivityTypeAll () {
		return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, activityTypeService.getTypeAll());
	}

}
